<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
    $pdo = Db::getPDO();
    try {
        $response = array('data'=> array());

        $configInvoice = ConfigInvoice::instance()->getOne($_GET['id']);
        $configInvoice['invoice'] = Table::instanceTable('Invoice')->getOne($configInvoice['invoiceId']);

        if($configInvoice['invoice']['type']=='colo')
            $configInvoice['invoice'] = array_merge($configInvoice['invoice'], Table::instanceTable('InvoiceColo')->getOne($configInvoice['invoiceId']));
        if($configInvoice['invoice']['type']=='vps')
            $configInvoice['invoice'] = array_merge($configInvoice['invoice'], Table::instanceTable('InvoiceVPS')->getOne($configInvoice['invoiceId']));
        if($configInvoice['invoice']['type']=='hosting')
            $configInvoice['invoice'] = array_merge($configInvoice['invoice'], Table::instanceTable('InvoiceHosting')->getOne($configInvoice['invoiceId']));

        $configInvoice['invoice']['account'] = Account::instance()->getOne($configInvoice['invoice']['accountId']);
        $configInvoice['invoice']['package'] = Package::instance()->getOne($configInvoice['invoice']['packageId']);

        $response['data']['configInvoice'] = $configInvoice;
        $response['data']['ips'] = IP::instance()->get();

        echo json_encode($response);
    } catch(Exception $ex) {
        echo json_encode(array('error'=> array(
            'code'=> $ex->getCode(),
            'message'=> $ex->getMessage()
        )));
        throw $ex;
    }
}
else if($_SERVER['REQUEST_METHOD']=='OPTIONS'){
    parse_str(file_get_contents("php://input"), $formData);
	if($formData['action'] = 'configSuccess'){
		try {
			$data = $formData['data'];
			ConfigInvoice::instance()->edit($data['id'], array('status'=> 1));
			echo json_encode(array('success'=> true));
		} catch(Exception $ex) {
			echo json_encode(array('success'=> false));
			throw $ex;
		}
	}
}
else if($_SERVER['REQUEST_METHOD']=='POST'){
    $pdo = Db::getPDO();
    $pdo->beginTransaction();
    try {
        ConfigInvoice::instance()->edit($_POST['id'], array('status'=> 1));

        $dateTimeout = new DateTime();
        $dateTimeout = $dateTimeout->add(new DateInterval('P'.$_POST['month'].'M'));

        Product::instance()->add(array(
            'dateTimeout'=> $dateTimeout->format('Y-m-d'),
            'type'=> $_POST['type'],
            'packageId'=> $_POST['packageId'],
            'accountId'=> $_POST['accountId'],
            'month'=> $_POST['month'],
            'note'=> $_POST['note'],
            'status'=> 1
        ));
        $id = $pdo->lastInsertId();
        if($_POST['type']=='colo')
            ProductColo::instance()->add(array(
                'id'=> $id,
                'ipId'=> $_POST['ipId']));

        else if($_POST['type']=='vps')
            ProductVPS::instance()->add(array(
                'id'=> $id,
                'ipId'=> $_POST['ipId'],
                'os'=> $_POST['os']
            ));
        else if($_POST['type']=='hosting')
            ProductHosting::instance()->add(array(
                'id'=> $id,
                'domain'=> $_POST['domain']
            ));
        Invoice::instance()->edit($_POST['invoiceId'], array('productId'=> $id));
        $pdo->commit();
        echo json_encode(array('success'=> true));
        $account = Account::instance()->getOne($_POST['accountId']);
        $package = Package::instance()->getOne($_POST['packageId']);
        Notify::setup($account['email'], $_POST['type'], $package['name'], $_POST['month'], $dateTimeout->format('Y-m-d'));
    }
    catch(Exception $e){
        $pdo->rollBack();
        echo json_encode(array(
            'error'=> array(
                'code'=> $e->getCode(),
                'message'=> $e->getMessage()
            )
        ));
        throw $e;
    }
}